package scsys.servlet;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import scsys.mapper.ElectiveMapper;
import scsys.mapper.UserMapper;
import scsys.pojo.Elective;
import scsys.pojo.User;
import scsys.util.SqlSessionFactoryUtils;

import java.util.List;

public class EletiveServiceImpl implements EletiveService {
    //1. 创建SqlSessionFactory 工厂对象
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();


    @Override
    public void add(Elective elective) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        ElectiveMapper mapper = sqlSession.getMapper(ElectiveMapper.class);

        //4. 调用方法
        mapper.add(elective);
        // 5、提交事務
        sqlSession.commit();
        //5. 释放资源
        sqlSession.close();
    }

    @Override
    public Elective selectOne(Elective elective) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        ElectiveMapper mapper = sqlSession.getMapper(ElectiveMapper.class);

        //4. 调用方法
        Elective selectElective = null;
        selectElective = mapper.selectOne(elective);

        //5. 释放资源
        sqlSession.close();

        return selectElective;
    }

    @Override
    public List<Elective> selectAll() {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        ElectiveMapper mapper = sqlSession.getMapper(ElectiveMapper.class);

        //4. 调用方法
        List<Elective> electives = null;
        electives = mapper.selectAll();

        //5. 释放资源
        sqlSession.close();
        return electives;
    }

    @Override
    public List<Elective> selectMore(Elective elective) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        ElectiveMapper mapper = sqlSession.getMapper(ElectiveMapper.class);

        //4. 调用方法
        List<Elective> electives = null;
        electives = mapper.selectMore(elective);

        //5. 释放资源
        sqlSession.close();
        return electives;
    }

    @Override
    public void deleteElective(Elective elective) {
        //2. 获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        ElectiveMapper mapper = sqlSession.getMapper(ElectiveMapper.class);

        //4. 调用方法
        mapper.deleteElective(elective);
        // 5、提交事務
        sqlSession.commit();
        //5. 释放资源
        sqlSession.close();
    }
}
